let db =require('../db');

// 获取物业列表
exports.propertyList = async function () {
    let sql=`
    SELECT
    ad.id,
    ad.userName,
    ad.phone,
    ad.role,
    ad.avatar,
    ad.status,
    ad.villageId,
    ad.password,
     DATE_FORMAT(ad.createdAt,'%Y-%m-%d %H:%i') AS createdAt,
     vi.name AS villageName
    FROM
    (SELECT*FROM  admin WHERE role ='property' AND isdelete='n') AS ad
    LEFT JOIN village AS vi ON vi.id =ad.villageId  
    `
    let data =await db.query(sql);
    return data[0];
}


// 验证手机号是否唯一

exports.validationPhone = async function(phone){
    let sql=`
     SELECT *FROM  admin  where phone =:phone
    `
    let data = await db.query(sql,{phone});
    return data[0];
}

//添加物业管理员

exports.addProperty = async function(name,pwd,phone,role,villageId){
let sql=`
 INSERT  INTO  admin(userName,password,phone,role,villageId)
 VALUES(:name,:pwd,:phone,:role,:villageId)
`
let data = await db.query(sql,{name,pwd,phone,role,villageId});
return data[0];
}

//修改物业管理员的信息

exports.updateProperty = async function(villageId,pwd,id){
let sql=`
UPDATE admin SET  
password =:pwd,
villageId =:villageId
where id =:id
`
let data =await db.query(sql,{villageId,pwd,id});
return data[0];
}


//删除
exports.propertyDel  = async function(id){
    let sql=`
    DELETE  FROM  admin where id=:id
    `
    let data = await db.query(sql,{id});
    return data[0];
}


//修改物业权限

exports.editAccess = async function(num,val){
    let sql =`
    UPDATE admin SET 
    status=:val
    where id =:num
    `
    let data = await db.query(sql,{num,val});
    return data[0];
}